﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommonLibrary.Web.Core
{
    /// <summary>
    /// Helper class for entities
    /// </summary>
    public class EntityHelper
    {
        /// <summary>
        /// gets the id via reflection
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static int GetId(object item)
        {
            var prop = item.GetType().GetProperty("Id");
            if (prop == null)
                return 0;
            var id = Convert.ToInt32(prop.GetValue(item, null));
            return id;
        }


        /// <summary>
        /// gets the id via reflection
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static void SetId(object item, int id)
        {
            var prop = item.GetType().GetProperty("Id");
            if (prop == null)
                return;
            prop.SetValue(item, id);
        }
    }
}
